package test.test_7_24;

import java.util.HashMap;
import java.util.Map;

/*
"aababcabcdabcde",获取字符串中每一个字母
出现的次数要求结果:a(5)b(4)c(3)d(2)e(1)
 */
public class Demo02 {
    public static void main(String[] args) {
        String str = "aababcabcdabcde";
        //创建Map
        //Key --> 字符
        //value --> 出现的次数
        HashMap<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < str.length(); i++) {
            // 取出每个字符
            char c = str.charAt(i);
            Integer count = map.getOrDefault(c, 0);
            map.put(c, ++count);
        }
        //遍历map
        StringBuffer sb = new StringBuffer();
        for (Map.Entry<Character, Integer> entry : map.entrySet()) {
            Character key = entry.getKey();
            Integer value = entry.getValue();
            //System.out.println("key = " + key + ",value = " + value);
            sb.append(key + "(" + value + ")");
        }
        System.out.println(sb);
    }
}
